package com.cn.appcore.ext

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.widget.ImageView
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.module.AppGlideModule
import jp.wasabeef.glide.transformations.BlurTransformation
import jp.wasabeef.glide.transformations.RoundedCornersTransformation

@GlideModule
class MyGlideAppModule : AppGlideModule()

fun ImageView.load(url:Any,builder: GlideRequest<Drawable>.()->Unit = {}){
    GlideApp.with(this)
        .load(url)
//        .placeholder(R.drawable.ic_default_error)
//        .error(R.drawable.ic_default_error)
//        .fallback(R.drawable.ic_default_error)
        .apply(builder)
        .into(this)
}


/**
 * 圆形图片
 * 直接调用circleCrop()
 */
@Deprecated("没有意义的方法，只是提醒一下圆形使用方式")
fun GlideRequest<Drawable>.circleCrop(){
    circleCrop()
}
/**
 * 图片圆角
 * radius 圆角角度，默认是5
 * cornerType 上左右下圆角，默认全圆角
 */
fun transformRoundCorner(
    radius: Int = 5,
    cornerType: RoundedCornersTransformation.CornerType = RoundedCornersTransformation.CornerType.ALL
): MultiTransformation<Bitmap> {
    //Glide设置圆角图片后设置ImageVIew的scanType="centerCrop"无效解决办法,将new CenterCrop()添加至此
    return MultiTransformation(
        CenterCrop(),
        RoundedCornersTransformation(radius, 0, cornerType)
    )
}

/**
 * 图片模糊
 */
fun transformBlur(blur:Int = 25,sampling:Int = 8): MultiTransformation<Bitmap> {
    return MultiTransformation(
        CenterCrop(),
        BlurTransformation(blur,sampling))
}

